package gui;

import java.util.ArrayList;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Canvas;

public class ClassShape extends Shape{
	private int MaxStrW;  //the width of the longest string
	private int ToLeng;  //Total Length of Strings that will be drawn in the rectangle
	private String name;  //the name of the class
	private int x;   //the x of the left top point of the class
	private int y;  //the y of the left top point of the class
	private int NumMethods;  //number of methods
	private int NumAttributes;  // number of attributes
	private ArrayList<String> Attributes=new ArrayList<String>();  //array list containing the attributes name
	private ArrayList<String> Methods=new ArrayList<String>();  //Array list containing the methods name
	Rectangle Name;
	Rectangle Container;   //the rectangle containing the name ,attributes & methods
	Line L1;   //separate class name & attributes
	Line L2;  //separate Attributes & Methods
	

	
	
	public ClassShape(String name1 , ArrayList<String> Attributes1 , ArrayList<String> Methods1) {  //constructor initializing the name , methods , attributes , number of methods & number of attributes
        name=name1;
		Attributes = Attributes1;
		Methods = Methods1;
		NumMethods = Methods1.size();
		NumAttributes = Attributes1.size();
		
	}
	
	
	public void getPosition(int x1, int y1){ // take the left top point of the class
		x=x1;
		y=y1;
	}
	public Point returnStart(){          // return the left top point of the class
		Point P1 = null;
		P1.x=this.x;
		P1.y=this.y;
		return P1;
	}
	public int returnlength(){          //return the rectangle length
		return this.ToLeng;
	}
	public int returnwidth(){          //return the rectangle width
		return this.MaxStrW;
	}


	@Override
	public void DrawShape(Canvas canvas) {   //Drawing the class
		GC gc=new GC(canvas);
		Point P1 = new Point(0, 0);
		
		Rectangle[] a1 = new Rectangle[NumAttributes];
		Rectangle[] m1 = new Rectangle[NumMethods];

		//calculating the length & width of the class
		P1 = gc.stringExtent(name); 
		MaxStrW = P1.x;
		ToLeng += P1.y;
		for(int i=0 ; i<Attributes.size() ; i++){
			String A = Attributes.get(i);
			P1 = gc.stringExtent(A);
			ToLeng += P1.y;                    //adding the height of each string
			if(P1.x > MaxStrW){                //find the maximum width
				MaxStrW = P1.x;
			}
		}
		for(int i=0 ; i<Methods.size() ; i++){
			String M = Methods.get(i);
			P1 = gc.stringExtent(M);
			ToLeng +=P1.y;
			if(P1.x > MaxStrW){                //find the maximum width
				MaxStrW = P1.x;
			}
		}
		
		
		MaxStrW += 10; //to leave space from the sides of the outer most rectangle
		ToLeng += 30; //to leave spaces between line & the start of writing methods & attributes
		
		//drawing the rectangle containing the methods , attributes & class name
		Container=new Rectangle(x, y, MaxStrW, ToLeng);
		Container.IsTransparent(false);
		Container.DrawShape(canvas);
		
		//Drawing Class Name
		P1=gc.textExtent(name);
		Name=new Rectangle(x+5, y+5, P1.x,P1.y);
		Name.IsTransparent(true);
		Name.DrawShape(canvas);
		gc.drawString(name, x+5, y+5);
		y+=(10+P1.y);
		L1=new Line(x, y, MaxStrW+x, y);
		L1.DrawShape(canvas);
		
				
		//Drawing Class Attributes
		y += 5;
		
		for(int i=0  ; i<Attributes.size() ; i++){
			String A = Attributes.get(i);
		    P1=gc.stringExtent(A);
		    a1[i]=new Rectangle(x+5, y, P1.x,P1.y);
		    a1[i].IsTransparent(true);
		    a1[i].DrawShape(canvas);
		    gc.drawString(A, x+5, y);
		    y +=P1.y;
		}
		
		y+=5;
		L2=new Line(x, y, MaxStrW+x, y);
		L2.DrawShape(canvas);
		
		//Drawing Methods
		
		y += 5;
		
		for(int i=0  ; i<Methods.size() ; i++){
			String M = Methods.get(i);
			P1=gc.stringExtent(M);
			m1[i]=new Rectangle(x+5, y, P1.x,P1.y);
			m1[i].IsTransparent(true);
			m1[i].DrawShape(canvas);
			gc.drawString(M, x+5, y);
			y += P1.y;
		}
					
	}

}

